package com.zhihuishu;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import com.huaban.analysis.jieba.WordDictionary;
import com.zhihuishu.domain.JiebaDto;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: wangjinliang
 * @Date: 2019/5/30 16:09
 */
public class JiebaDemo {
    private static final Map<String, Integer> extMap = new ConcurrentHashMap<>();

    /**
     * 需要排除的词性
     */
    static {
        extMap.put("c", -1);
        extMap.put("d", -1);
        extMap.put("e", -1);
        extMap.put("f", -1);
        extMap.put("m", -1);
        extMap.put("p", -1);
        extMap.put("q", -1);
        extMap.put("r", -1);
        extMap.put("u", -1);
        extMap.put("ug", -1);
        extMap.put("ud", -1);
        extMap.put("ul", -1);
        extMap.put("uj", -1);
        extMap.put("uz", -1);
        extMap.put("y", -1);
        extMap.put("zg", -1);
    }

    public static void main(String[] args) throws Exception {
        String filePath1 = "E:\\workplace\\aliyunlog\\jieba\\1.txt";
        List<String> questionListOne = Utils.getListByFile(filePath1);
        String filePath2 = "E:\\workplace\\aliyunlog\\jieba\\2.txt";
        List<String> questionListTwo = Utils.getListByFile(filePath2);
        String filePath3 = "E:\\workplace\\aliyunlog\\jieba\\3.txt";
        List<String> questionListThree = Utils.getListByFile(filePath3);
        String filePath4 = "E:\\workplace\\aliyunlog\\jieba\\4.txt";
        List<String> questionListFour = Utils.getListByFile(filePath4);

        dealMessageContent(questionListOne);
        dealMessageContent(questionListTwo);
        dealMessageContent(questionListThree);
        dealMessageContent(questionListFour);

//        List<String> all = new ArrayList<>();
//        all.addAll(questionListOne);
//        all.addAll(questionListTwo);
//        all.addAll(questionListThree);
//        all.addAll(questionListFour);
//        dealMessageContent(all);
    }

    private static void dealMessageContent(List<String> contents) {
        URL resource = JiebaBaseDemo.class.getClassLoader().getResource("dicts/jieba.dict");
        if (Objects.isNull(resource)) {
            throw new RuntimeException("dicts/jieba.dict not found");
        }
        File file = new File(resource.getPath());
        WordDictionary.getInstance().loadUserDict(file);

        Map<String, JiebaDto> resultMap = new HashMap<>();
        Map<String, Set<String>> wordMap = new HashMap<>();
        JiebaSegmenter segmenter = new JiebaSegmenter();
        contents.forEach(item -> {
            List<SegToken> segTokenList = segmenter.process(item, JiebaSegmenter.SegMode.SEARCH);
            segTokenList.forEach(segToken -> {
                if (StringUtils.isNotBlank(segToken.word.getTokenType()) && extMap.get(segToken.word.getTokenType()) == null && segToken.word.getToken().length() > 1) {
                    JiebaDto temp = resultMap.get(segToken.word.getToken());
                    if (temp != null) {
                        temp.setCount(temp.getCount() + 1);
                        resultMap.put(segToken.word.getToken(), temp);
                    } else {
                        JiebaDto dto = new JiebaDto();
                        dto.setCount(1);
                        dto.setMessage(segToken.word);
                        resultMap.put(segToken.word.getToken(), dto);
                    }

                    Set<String> wordList = wordMap.get(segToken.word.getTokenType());
                    if (wordList != null) {
                        wordList.add(segToken.word.getToken());
                        wordMap.put(segToken.word.getTokenType(), wordList);
                    } else {
                        wordList = new TreeSet<>();
                        wordList.add(segToken.word.getToken());
                        wordMap.put(segToken.word.getTokenType(), wordList);
                    }
                }
            });
        });
        List<JiebaDto> resultList = new ArrayList<>(resultMap.size());
        resultList.addAll(resultMap.values());
        resultList.sort(Comparator.comparing(JiebaDto::getCount).reversed());
        printResult(resultList);
//        printResultV2(wordMap);
    }

    private static void printResultV2(Map<String, Set<String>> wordMap) {
        wordMap.entrySet().forEach(item -> {
            String key = item.getKey();
            Set<String> values = item.getValue();
            System.out.println(key + "  " + values);
        });
    }

    private static void printResult(List<JiebaDto> resultList) {
        resultList.forEach(dto -> {
            System.out.println(dto.getMessage().getToken() + "  " + dto.getMessage().getTokenType());
        });
        System.out.println("================================");
    }
}
